package com.ruoyi.business.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusMaintenanceMapper;
import com.ruoyi.business.domain.BusMaintenance;
import com.ruoyi.business.service.IBusMaintenanceService;
import javax.annotation.Resource;

/**
 * 维保任务Service业务层处理
 * 
 * @author wf
 * @date 2024-07-18
 */
@Service
public class BusMaintenanceServiceImpl implements IBusMaintenanceService 
{
    @Resource
    private BusMaintenanceMapper busMaintenanceMapper;
    @Resource
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询维保任务
     * 
     * @param id 维保任务主键
     * @return 维保任务
     */
    @Override
    public BusMaintenance selectBusMaintenanceById(Long id)
    {
        return busMaintenanceMapper.selectBusMaintenanceById(id);
    }

    /**
     * 查询维保任务列表
     * 
     * @param busMaintenance 维保任务
     * @return 维保任务
     */
    @Override
    public List<BusMaintenance> selectBusMaintenanceList(BusMaintenance busMaintenance)
    {
        return busMaintenanceMapper.selectBusMaintenanceList(busMaintenance);
    }

    /**
     * 新增维保任务
     * 
     * @param busMaintenance 维保任务
     * @return 结果
     */
    @Override
    public int insertBusMaintenance(BusMaintenance busMaintenance)
    {
        busMaintenance.setCreateUser(SecurityUtils.getUserId());
        busMaintenance.setCreateBy(SecurityUtils.getUsername());
        busMaintenance.setCreateTime(DateUtils.getNowDate());
        busMaintenance.setUpdateUser(SecurityUtils.getUserId());
        busMaintenance.setUpdateBy(SecurityUtils.getUsername());
        busMaintenance.setUpdateTime(DateUtils.getNowDate());
        return busMaintenanceMapper.insertBusMaintenance(busMaintenance);
    }

    /**
     * 新增维保任务 添加所有设备
     *
     * @param list 维保任务
     * @return 结果
     */
    @Override
    public int insertAllBusMaintenance(List<BusMaintenance> list) {

        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            BusMaintenanceMapper busMaintenanceMapper = sqlSession.getMapper(BusMaintenanceMapper.class);
            list.stream().forEach(busMaintenance -> busMaintenanceMapper.insertBusMaintenance(busMaintenance));
            // 提交数据
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return 200;
    }

    /**
     * 修改维保任务
     * 
     * @param busMaintenance 维保任务
     * @return 结果
     */
    @Override
    public int updateBusMaintenance(BusMaintenance busMaintenance)
    {
        busMaintenance.setUpdateUser(SecurityUtils.getUserId());
        busMaintenance.setUpdateBy(SecurityUtils.getUsername());
        busMaintenance.setUpdateTime(DateUtils.getNowDate());
        return busMaintenanceMapper.updateBusMaintenance(busMaintenance);
    }

    /**
     * 批量删除维保任务
     * 
     * @param ids 需要删除的维保任务主键
     * @return 结果
     */
    @Override
    public int deleteBusMaintenanceByIds(Long[] ids)
    {
        return busMaintenanceMapper.deleteBusMaintenanceByIds(ids);
    }

    /**
     * 删除维保任务信息
     * 
     * @param id 维保任务主键
     * @return 结果
     */
    @Override
    public int deleteBusMaintenanceById(Long id)
    {
        return busMaintenanceMapper.deleteBusMaintenanceById(id);
    }

    @Override
    public Integer getMaintenanceCount() {
        return busMaintenanceMapper.getMaintenanceCount();
    }

    /**
     * 获取维保任务总数和已完成维保任务的数量
     *
     * @return {@link Long[] }
     * @author admin
     * @date 2024/10/16
     */
    @Override
    public Long[] getMaintenanceAndCompleted() {
        Map<String, Long> countsMap = busMaintenanceMapper.getMaintenanceAndCompleted();
        return new Long[]{countsMap.get("totalCount"), countsMap.get("completedCount")};
    }
}
